package com.tsingtree.gen.core.generator.ts;

import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.tsingtree.gen.core.generator.base.model.ContextParam;
import com.tsingtree.gen.core.generator.ts.controller.ControllerGenerator;
import com.tsingtree.gen.core.generator.ts.html.PageAddGenerator;
import com.tsingtree.gen.core.generator.ts.html.PageEditGenerator;
import com.tsingtree.gen.core.generator.ts.html.PageIndexGenerator;
import com.tsingtree.gen.core.generator.ts.js.PageAddJsGenerator;
import com.tsingtree.gen.core.generator.ts.js.PageEditJsGenerator;
import com.tsingtree.gen.core.generator.ts.js.PageIndexJsGenerator;
import com.tsingtree.gen.core.generator.ts.mybatisplus.MpGenerator;
import com.tsingtree.gen.core.generator.ts.mybatisplus.param.MpParams;
import com.tsingtree.gen.core.generator.ts.sqls.MenuSqlGenerator;

import java.util.List;
import java.util.Map;

/**
 * 测试的执行器
 *
 * @author jagger
 */
public class Executor {

    /**
     * 默认的生成器
     *
     * @author jagger
     */
    public static void executor(ContextParam contextParam, MpParams mpContext) {

        //执行mp的代码生成，生成entity,dao,service,model，生成后保留数据库元数据
        MpGenerator mpGenerator = new MpGenerator(mpContext);
        mpGenerator.initContext(contextParam);
        mpGenerator.doGeneration();

        //获取元数据
        List<TableInfo> tableInfos = mpGenerator.getTableInfos();
        Map<String, Map<String, Object>> everyTableContexts = mpGenerator.getEveryTableContexts();

        //遍历所有表
        for (TableInfo tableInfo : tableInfos) {
            Map<String, Object> map = everyTableContexts.get(tableInfo.getName());

            //生成控制器
            ControllerGenerator controllerGenerator = new ControllerGenerator(map);
            controllerGenerator.initContext(contextParam);
            controllerGenerator.doGeneration();

            //生成主页面html
            PageIndexGenerator pageIndexGenerator = new PageIndexGenerator(map);
            pageIndexGenerator.initContext(contextParam);
            pageIndexGenerator.doGeneration();

            //生成主页面js
            PageIndexJsGenerator pageIndexJsGenerator = new PageIndexJsGenerator(map);
            pageIndexJsGenerator.initContext(contextParam);
            pageIndexJsGenerator.doGeneration();

            //生成添加页面html
            PageAddGenerator pageAddGenerator = new PageAddGenerator(map);
            pageAddGenerator.initContext(contextParam);
            pageAddGenerator.doGeneration();

            //生成添加页面的js
            PageAddJsGenerator pageAddJsGenerator = new PageAddJsGenerator(map);
            pageAddJsGenerator.initContext(contextParam);
            pageAddJsGenerator.doGeneration();

            //生成编辑页面html
            PageEditGenerator pageEditGenerator = new PageEditGenerator(map);
            pageEditGenerator.initContext(contextParam);
            pageEditGenerator.doGeneration();

            //生成编辑页面的js
            PageEditJsGenerator pageEditJsGenerator = new PageEditJsGenerator(map);
            pageEditJsGenerator.initContext(contextParam);
            pageEditJsGenerator.doGeneration();

            //生成菜单的sql
            MenuSqlGenerator menuSqlGenerator = new MenuSqlGenerator(map);
            menuSqlGenerator.initContext(contextParam);
            menuSqlGenerator.doGeneration();
        }
    }

}
